using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Fieldbook.Entities;
using System.Collections.Generic;
using Fieldbook.Business;

namespace Fieldbook.Testwebsite.User
{
    public partial class FieldSpecies : FieldbookPage
    {
        protected List<PlantSpecie> allSpecies = null;
         
        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
            if (selectPlantSpecieCategory.Items.Count == 0)
            {
                allSpecies = PlantSpecieController.LoadAllPlantSpecies();

                foreach (PlantSpecie.PlantCategory value in PlantSpecie.PlantCategory.GetValues(typeof(PlantSpecie.PlantCategory)))
                {
                    selectPlantSpecieCategory.Items.Add(
                        new ListItem(value.ToString(),
                            ((byte)value).ToString()));
                }
            }
        }

        /// <summary>
        ///  Adding new specie to field
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void linkAddFieldSpecie_Command(object sender, CommandEventArgs e)
        {
            FieldPlantSpecie fps = new FieldPlantSpecie();
            long rqFieldID = Int32.Parse(Request["fieldID"]);
            long? fieldID = FieldController.CheckHouseHoldField((long)Session["userID"], rqFieldID);
            if (fieldID == null)
            {
                this.lblOperationStatus.Text = "User is not autorised to edit current field";
                return;
            }
            fps.FieldID = (long)fieldID;
            fps.PlantSpecieID = Int64.Parse(Request["selectSpecieID"]);
            fps.Sort = this.txtSort.Text;
            fps.SpecieStartDate = DateTime.Parse(this.txtSpecieStartDate.Text);

            bool sucess = PlantSpecieController.InsertFieldPlantSpecie(fps);
            if (sucess)
            {
                this.lblOperationStatus.Text = "Field specie sucessfuflly addded";
                this.txtSort.Text = "";
                this.txtSpecieStartDate.Text = "";
                this.dgvFieldWorks.DataBind();
            }
            else 
            {
                this.lblOperationStatus.Text = "Error inserting field specie";
            }
            allSpecies = PlantSpecieController.LoadAllPlantSpecies();
        }
    }
}
